package bubblesort;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by status200 on 2018/4/4.
 */
public class BubbleSort {

    public static void main(String[] args) {

        Integer[] arr = {123,446,3657,3121,65875,4351,65,980};

        System.out.println(Arrays.toString(arr));

        bubbleSort(arr, Comparator.comparingInt(o -> o));

        System.out.println(Arrays.toString(arr));
    }

    public static <T> void bubbleSort(T[] arr, Comparator<T> comparator) {

        for(int i = 0;i < arr.length;i++) {
            for(int j = 0;j < arr.length - 1 - i;j++) {
                if(comparator.compare(arr[j], arr[j+1]) > 0) {
                    swap(arr,j,j+1);
                }
            }
        }
    }

    public static <T> void swap(T[] arr, int i, int j) {
        T temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


}
